using Newtonsoft.Json;
using NExt.Serialization.Core;

namespace NExt.Serialization.JsonNet
{
    public class JsonNetSerializer : IJsonSerializer
    {
        private readonly JsonSerializerSettings jsonSerializerSettings;

        public JsonNetSerializer(JsonSerializerSettings jsonSerializerSettings)
        {
            this.jsonSerializerSettings = jsonSerializerSettings;
        }

        public string Serialize(object instance)
        {
            string serializedInstance = JsonConvert.SerializeObject(instance, Formatting.None, jsonSerializerSettings);
            return serializedInstance;
        }

        public T Deserialize<T>(string json)
        {
            T instance = JsonConvert.DeserializeObject<T>(json, jsonSerializerSettings);
            return instance;
        }

        public object Deserialize(string json, System.Type type)
        {
            object instance = JsonConvert.DeserializeObject(json, type, jsonSerializerSettings);
            return instance;
        }
    }
}